package com.xjrsoft.module.customerTwo.ycode.t_ycode_label_printing_record.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.entity.XjrBaseAnnexesFile;
import com.xjrsoft.module.base.entity.XjrBaseUser;
import com.xjrsoft.module.base.service.IXjrBaseAnnexesFileService;
import com.xjrsoft.module.base.service.IXjrBaseUserService;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_label_printing_record.dto.TYcodeLabelPrintingRecordDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_label_printing_record.dto.SaveTYcodeLabelPrintingRecordFormDataDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_label_printing_record.vo.TYcodeLabelPrintingRecordVo;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_label_printing_record.vo.TYcodeLabelPrintingRecordListVo;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_label_printing_record.dto.TYcodeLabelPrintingRecordListDto;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.entity.TYcodeProduct;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_product.service.ITYcodeProductService;
import com.xjrsoft.module.customerTwo.ycode.utils.YCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_label_printing_record.entity.TYcodeLabelPrintingRecord;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_label_printing_record.vo.TYcodeLabelPrintingRecordVo;
import com.xjrsoft.module.customerTwo.ycode.t_ycode_label_printing_record.service.ITYcodeLabelPrintingRecordService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2024-08-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("/t_ycode_label_printing_record")
@Api(value = "标签打印记录", tags = "标签打印记录接口")
public class TYcodeLabelPrintingRecordController {


	private final ITYcodeLabelPrintingRecordService tYcodeLabelPrintingRecordService;

	private final ITYcodeProductService tYcodeProductService;

	private final IXjrBaseUserService iXjrBaseUserService;

	private IXjrBaseAnnexesFileService annexesFileService;

	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tYcodeLabelPrintingRecord")
	@SaCheckPermission(value = {"t_ycode_label_printing_record:view", "t_ycode_label_printing_record:modify"}, mode = SaMode.OR)
	public Response<TYcodeLabelPrintingRecordVo> getFormData(@PathVariable String id) {
		// 主表数据
		TYcodeLabelPrintingRecord tYcodeLabelPrintingRecord = tYcodeLabelPrintingRecordService.getById(id);
		TYcodeLabelPrintingRecordVo tYcodeLabelPrintingRecordVo = BeanUtil.copy(tYcodeLabelPrintingRecord, TYcodeLabelPrintingRecordVo.class);
		return Response.ok(tYcodeLabelPrintingRecordVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tYcodeLabelPrintingRecord")
		public Response<PageOutput<TYcodeLabelPrintingRecordListVo>> getPageList(TYcodeLabelPrintingRecordListDto listDto) {
		IPage<TYcodeLabelPrintingRecord> page = tYcodeLabelPrintingRecordService.getPageList(listDto);
		List<TYcodeLabelPrintingRecordListVo> records = BeanUtil.copyList(page.getRecords(), TYcodeLabelPrintingRecordListVo.class);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping("/savePrintRecord")
	@ApiOperation(value = "新增", notes = "传入tYcodeLabelPrintingRecord")
	public Response savePrintRecord(@RequestBody TYcodeLabelPrintingRecordDto formDto) {
		TYcodeLabelPrintingRecord entity = BeanUtil.copy(formDto,TYcodeLabelPrintingRecord.class);
		if(StringUtils.isNotBlank(formDto.getImg_data())){
			//base64转图片
			String id = IdWorker.get32UUID();
			XjrBaseAnnexesFile file = YCodeUtils.convertBase64ToImage(formDto.getImg_data(),id+".png");
			file.setId(id);
			file.setFolderId(id);
			annexesFileService.save(file);
			entity.setAttachment(id);
		}
		return Response.status(tYcodeLabelPrintingRecordService.addTYcodeLabelPrintingRecord(entity));
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入tYcodeLabelPrintingRecord")
	@SaCheckPermission("t_ycode_label_printing_record:modify")
	public Response update(@PathVariable String id, @RequestBody SaveTYcodeLabelPrintingRecordFormDataDto formDto) {
		TYcodeLabelPrintingRecordDto tYcodeLabelPrintingRecordDto = formDto.getTYcodeLabelPrintingRecordDto();
		TYcodeLabelPrintingRecord tYcodeLabelPrintingRecord = BeanUtil.copy(tYcodeLabelPrintingRecordDto, TYcodeLabelPrintingRecord.class);
		return Response.status(tYcodeLabelPrintingRecordService.updateTYcodeLabelPrintingRecord(id, tYcodeLabelPrintingRecord));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"t_ycode_label_printing_record:batchDelete", "t_ycode_label_printing_record:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(tYcodeLabelPrintingRecordService.delete(ids));
	}

	@GetMapping("/getEntityByProductId")
	public Response<TYcodeLabelPrintingRecordVo> getEntityByProductId(@RequestParam("product_id") String id) {
		// 主表数据
		TYcodeLabelPrintingRecordVo vo = new TYcodeLabelPrintingRecordVo();
		TYcodeProduct product = tYcodeProductService.getById(id);
		if(product != null){
			vo.setProductCode(product.getCode());
			vo.setProductName(product.getName());
			vo.setProductInternalCode(product.getInternalCode());
			vo.setProductSpecifications(product.getSpecifications());
			XjrBaseUser user = iXjrBaseUserService.getById(product.getMerchantId());
			if(null != user){
				vo.setFirmName(user.getFFirmname());
			}
			//根据产品云码生成二维码
//			vo.setRqCode(YCodeUtils.createQRCode(vo.getProductCode()));
		}
		return Response.ok(vo);
	}

}
